/**
 * 
 */
package org.xlab.baseframework.webservice.impl;

import javax.jws.WebService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.xlab.baseframework.annotation.Security;
import org.xlab.baseframework.common.entity.AbstractSystemBean;
import org.xlab.baseframework.domain.enumerate.EnumWebServiceClientType;
import org.xlab.baseframework.exception.PermissionDeniedException;
import org.xlab.baseframework.exception.RootUserNotFoundException;
import org.xlab.baseframework.exception.SystemRuntimeException;
import org.xlab.baseframework.exception.SystemWebServiceSessionNotFoundException;
import org.xlab.baseframework.service.api.ISystemWebServiceSessionService;
import org.xlab.baseframework.webservice.api.ISessionWebService;

/**
 * @author makubex
 * 
 */
@WebService(endpointInterface = "org.xlab.baseframework.webservice.api.ISessionWebService")
public class SessionWebServiceImpl extends AbstractSystemBean implements
		ISessionWebService {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = -8214418255425770335L;

	private static final Logger log = Logger
			.getLogger(SessionWebServiceImpl.class);

	@Autowired(required = true)
	@Qualifier("bpf_systemWSSessionService")
	private ISystemWebServiceSessionService systemWebServiceSessionService;

	/**
	 * @return the systemWebServiceSessionService
	 */
	public ISystemWebServiceSessionService getSystemWebServiceSessionService() {
		return systemWebServiceSessionService;
	}

	/**
	 * @throws PermissionDeniedException
	 * @throws SystemWebServiceSessionNotFoundException
	 * @see org.xlab.baseframework.webservice.api.ISessionWebService#ping(java.lang.String,
	 *      java.lang.String)
	 */
	@Security
	public void ping(String sessionId, String clientId)
			throws SystemWebServiceSessionNotFoundException,
			PermissionDeniedException {
		log.debug("Session[" + sessionId + "] from[" + clientId
				+ "] pinging server...");
		systemWebServiceSessionService.updateStatus(sessionId, clientId);
	}

	/**
	 * @param systemWebServiceSessionService
	 *            the systemWebServiceSessionService to set
	 */
	public void setSystemWebServiceSessionService(
			ISystemWebServiceSessionService systemWebServiceSessionService) {
		this.systemWebServiceSessionService = systemWebServiceSessionService;
	}

	/**
	 * @see org.xlab.baseframework.webservice.api.ISessionWebService#simpleLogin(java.lang.String)
	 */
	public String simpleLogin(String clientId) {
		try {
			return this.systemWebServiceSessionService.createSession(
					super.userService.getSystemRootUser(), clientId,
					EnumWebServiceClientType.BROWSER);
		} catch (RootUserNotFoundException e) {
			throw new SystemRuntimeException(e);
		}
	}

}
